package cn.yzfy.common.validate.impl;

import cn.yzfy.common.validate.ValidateNotChinese;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Set;

public class NotNullAndEmptyValidatorImpl
    implements ConstraintValidator<ValidateNotChinese.ValidateNotNullAndEmpty, Object> {
  public NotNullAndEmptyValidatorImpl() {
  }
  
  @Override
  public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {
    if (o instanceof List) {
      return !((List<?>) o).isEmpty();
    } else if (o instanceof Set) {
      return !((Set<?>) o).isEmpty();
    } else if (o instanceof String) {
      return !o.toString().isEmpty();
    } else if (o instanceof MultipartFile) {
      return !((MultipartFile) o).isEmpty();
    } else {
      return o instanceof Long || o instanceof Integer || o instanceof BigDecimal ||
          o instanceof Boolean || o instanceof Double || o instanceof Float ||
          o instanceof Enum || o instanceof Date;
    }
  }
  
  @Override
  public void initialize(ValidateNotChinese.ValidateNotNullAndEmpty constraintAnnotation) {
    ConstraintValidator.super.initialize(constraintAnnotation);
  }
}